package com.masterclient.app.modules.income.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.widget.RelativeLayout.ALIGN_PARENT_LEFT;
import static android.widget.RelativeLayout.ALIGN_PARENT_RIGHT;

/**
 * @auther hjian
 * @date 2020/6/16.
 */
public class ProfitLineRatioView extends LinearLayout {

    @BindView(R.id.profit_lineratio_ratio)
    TextView profitLineratioRatio;
    @BindView(R.id.profit_lineratio_bubble)
    View profitLineratioBubble;
    @BindView(R.id.profit_lineratio_bubblelayout)
    RelativeLayout profitLineratioBubblelayout;
    @BindView(R.id.profit_lineratio_left)
    View profitLineratioLeft;
    @BindView(R.id.profit_lineratio_right)
    View profitLineratioRight;
    @BindView(R.id.profit_lineratio_leftbg)
    View profitLineratioLeftbg;
    @BindView(R.id.profit_lineratio_rightbg)
    View profitLineratioRightbg;
//    @BindView(R.id.profit_lineratio_leftline)
//    View profitLineratioLefline;
//    @BindView(R.id.profit_lineratio_rightline)
//    View profitLineratioRightline;

    private Context context;
    private String leftContent, rightContent;
    private int leftRatio, rightRatio; // 和为100

    public ProfitLineRatioView(Context context) {
        this(context, null);
    }

    public ProfitLineRatioView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        inflate(context, R.layout.profit_line_ratio, this);
        ButterKnife.bind(this);
        ratioWidth = ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(60);

        profitLineratioBubblelayout.setVisibility(View.INVISIBLE);
    }

    private int ratioWidth;
    private LinearLayout.LayoutParams layoutParamsLeft, layoutParamsRight;
    private RelativeLayout.LayoutParams layoutParamsRatio, layoutParamsBubble;

    public void setData(String leftContent, String rightContent, int leftRatio, int rightRatio) {

        this.leftContent = leftContent;
        this.rightContent = rightContent;
        this.leftRatio = leftRatio;
        this.rightRatio = rightRatio;

        layoutParamsLeft = (LinearLayout.LayoutParams) profitLineratioLeft.getLayoutParams();
        layoutParamsRight = (LinearLayout.LayoutParams) profitLineratioRight.getLayoutParams();
        layoutParamsRatio = (RelativeLayout.LayoutParams) profitLineratioRatio.getLayoutParams();
        layoutParamsBubble = (RelativeLayout.LayoutParams) profitLineratioBubble.getLayoutParams();


        layoutParamsLeft.weight = leftRatio;
        layoutParamsRight.weight = rightRatio;

        if (leftRatio == 0 || rightRatio == 0) {
            profitLineratioLeft.setVisibility(leftRatio == 0 ? GONE : VISIBLE);
            profitLineratioRight.setVisibility(rightRatio == 0 ? GONE : VISIBLE);
            profitLineratioLeftbg.setBackgroundResource(R.drawable.shape_round_ff3b64);
            profitLineratioRightbg.setBackgroundResource(R.drawable.shape_round_a25dff);
        } else {
            profitLineratioLeft.setVisibility(VISIBLE);
            profitLineratioRight.setVisibility(VISIBLE);
            profitLineratioLeftbg.setBackgroundResource(R.drawable.shape_round_ff3b64_left);
            profitLineratioRightbg.setBackgroundResource(R.drawable.shape_round_a25dff_right);
        }

//        switchRatio(leftRatio != 0);
        if (leftRatio == 0 || rightRatio == 0) {
            profitLineratioLeftbg.setBackgroundResource(R.drawable.shape_round_ff3b64_click);
            profitLineratioRightbg.setBackgroundResource(R.drawable.shape_round_a25dff_click);
        }else {
            profitLineratioLeftbg.setBackgroundResource(R.drawable.shape_round_ff3b64_left_click);
            profitLineratioRightbg.setBackgroundResource(R.drawable.shape_round_a25dff_right_click);
        }
        switchRatio(leftRatio != 0);
//        profitLineratioLefline.setVisibility(View.VISIBLE);
//        profitLineratioRightline.setVisibility(View.VISIBLE);
//        profitLineratioBubblelayout.setVisibility(View.INVISIBLE);
//        invalidate();
    }


    @OnClick({R.id.profit_lineratio_left, R.id.profit_lineratio_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.profit_lineratio_left:
                switchRatio(true);
                break;

            case R.id.profit_lineratio_right:
                switchRatio(false);
                break;
        }
    }

    private void switchRatio(boolean isleft) {

        layoutParamsBubble.leftMargin = 0;
        layoutParamsRatio.leftMargin = 0;
        layoutParamsBubble.rightMargin = 0;
        layoutParamsRatio.rightMargin = 0;
        layoutParamsRatio.getRules()[ALIGN_PARENT_LEFT] = 0;
        layoutParamsBubble.getRules()[ALIGN_PARENT_LEFT] = 0;
        layoutParamsRatio.getRules()[ALIGN_PARENT_RIGHT] = 0;
        layoutParamsBubble.getRules()[ALIGN_PARENT_RIGHT] = 0;

        if (isleft) {
            profitLineratioRatio.setText(leftContent);
            if (leftRatio == 100 ){
                profitLineratioRightbg.setBackgroundResource(R.drawable.shape_round_a25dff_click);
                profitLineratioLeftbg.setBackgroundResource(R.drawable.shape_round_ff3b64);
            }else {
                profitLineratioRightbg.setBackgroundResource(R.drawable.shape_round_a25dff_right_click);
                profitLineratioLeftbg.setBackgroundResource(R.drawable.shape_round_ff3b64_left);
            }
//            profitLineratioLefline.setVisibility(View.GONE);
//            profitLineratioRightline.setVisibility(View.VISIBLE);
            layoutParamsRatio.addRule(ALIGN_PARENT_LEFT);
            layoutParamsBubble.addRule(ALIGN_PARENT_LEFT);
            layoutParamsBubble.leftMargin = ratioWidth * leftRatio / 200 + ScreenUtils.dp2px(5);
            layoutParamsRatio.leftMargin = ratioWidth * leftRatio / 200 - ScreenUtils.dp2px(5);

        } else {

            profitLineratioRatio.setText(rightContent);

            if (leftRatio == 100 ){
                profitLineratioRightbg.setBackgroundResource(R.drawable.shape_round_a25dff);
                profitLineratioLeftbg.setBackgroundResource(R.drawable.shape_round_ff3b64_click);

            }else {
                profitLineratioRightbg.setBackgroundResource(R.drawable.shape_round_a25dff_right);
                profitLineratioLeftbg.setBackgroundResource(R.drawable.shape_round_ff3b64_left_click);
            }

//            profitLineratioLefline.setVisibility(View.VISIBLE);
//            profitLineratioRightline.setVisibility(View.GONE);
            layoutParamsRatio.addRule(ALIGN_PARENT_RIGHT);
            layoutParamsBubble.addRule(ALIGN_PARENT_RIGHT);
            layoutParamsBubble.rightMargin = ratioWidth * rightRatio / 200 + ScreenUtils.dp2px(5);
            layoutParamsRatio.rightMargin = ratioWidth * rightRatio / 200 - ScreenUtils.dp2px(5);


        }

        profitLineratioBubblelayout.setVisibility(View.VISIBLE);
        invalidate();
    }
}
